package com.abing.starry.config.pool;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author CaptainBing
 * @Date 2024/8/21 17:28
 * @Description
 */
@Configuration
public class ThreadPoolConfig {

    @Bean("emailThreadPool")
    public ThreadPoolExecutor threadPoolExecutor(){
        int coreSize = 2;
        int maximumPoolSize = 4;
        int workCapacity = 2;
        CustomizableThreadFactory customizableThreadFactory = new CustomizableThreadFactory("email-thread-");
        return new ThreadPoolExecutor(coreSize, maximumPoolSize,
                                      2L, TimeUnit.MINUTES,
                                      new ArrayBlockingQueue<>(workCapacity),
                                      customizableThreadFactory,
                                      new ThreadPoolExecutor.AbortPolicy());

    }

}
